package com.example.bookrecommendationjava.util;

import com.example.bookrecommendationjava.mapper.BookMapper;
import com.example.bookrecommendationjava.model.Book;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: book-recommendation-java
 * @BelongsPackage: com.example.bookrecommendationjava.util
 * @Author: ZhShy
 * @CreateTime: 2022-03-14 17:47
 * @Description:
 */

@Controller
@RequestMapping("reptile")
@ResponseBody
public class Reptile {
    @Autowired
    BookMapper bookMapper;

    @RequestMapping("get")
    @ResponseBody
    public List<Book> Index() throws Exception {
        //获取url请求
        ArrayList<Book> bookList = new ArrayList<Book>();

        String url = "https://book.douban.com/latest?subcat=%E5%85%A8%E9%83%A8&p=";
        for (int i = 0; i < 10; i++) {
            url = "https://book.douban.com/latest?subcat=%E5%85%A8%E9%83%A8&p=";
            url += i;
            //解析网页,Jsoup返回的是Document对象（浏览器Document对象）
//        Document document = Jsoup.parse(new URL(url), 30000);
            Connection conn = Jsoup.connect(url).timeout(5000);
            conn.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            conn.header("Accept-Encoding", "gzip, deflate, sdch");
            conn.header("Accept-Language", "zh-CN,zh;q=0.8");
            conn.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
            //所有在js中使用的方法，这里都能使用
//        Element element = document.getElementById("content");
            Element element = conn.get();
            //在获得网页内容后，获取所有的li标签
            Elements elements = element.getElementsByClass("media clearfix");


            //获取元素的标签后，再获取标签中的内容
            for (Element el : elements) {
                String bookUrl = el.getElementsByClass("fleft").attr("href");
                String bookImgUrl = el.getElementsByTag("img").attr("src");
                String bookName = el.getElementsByTag("h2").eq(0).text();
                String bookAuthor = el.getElementsByClass("color-gray").eq(0).text();

                Book book = new Book();
                book.setBookUrl(bookUrl);
                book.setBookImgUrl(bookImgUrl);
                book.setBookName(bookName);
                book.setBookAuthor(bookAuthor);
                bookMapper.insertIntoBook(book);
                bookList.add(book);
            }
        }
        return bookList;
    }
}
